package com.dimeng;

import java.util.HashMap;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.dimeng.entity.Info;
import com.dimeng.utils.AppCache;
import com.dimeng.utils.CommonUtil;
import com.dimeng.utils.Constant;

public class BrowsePriceActivity extends SuperActivity{
	private static final long serialVersionUID = 7822565887374225617L;
	private TextView catInfoTitle;
	WebView webView;
	String mimeType = "text/html";
	String charset = Constant.CHARSET_UTF8;
	private ImageButton nextInfoButton;
	private int location = 0;
	private Info info;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.browse_info_webview);
		catInfoTitle = (TextView) this.findViewById(R.id.catInfoTitle);
		catInfoTitle.setText(getResources().getString(R.string.price));
		webView = (WebView) this.findViewById(R.id.catInfoWebView);
		
		Intent intent = getIntent();
		info = (Info) intent.getSerializableExtra("info");
		location = intent.getIntExtra("location", 0);
		charset = intent.getStringExtra("charset");
		String content = "<center><h3>"+info.getTitle()+"</h3>作者:"+info.getAuthor() + "   来源:"+info.getSource()+"</center>";
		content += info.getContent();
		CommonUtil.loadData(this, content, "text/html", charset,webView, true, true, false);
		
		nextInfoButton = (ImageButton) findViewById(R.id.nextInfoButton);
		CommonUtil.setButtonFocusChanged(nextInfoButton);
		nextInfoButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if(location+1 > (AppCache.keyListMap.get(AppCache.PRICE).size()-1)){//即下一篇资讯的位置
					Toast.makeText(BrowsePriceActivity.this, "已经是最后一篇文章了", Toast.LENGTH_SHORT).show();
				} else {
					new Thread(){
						public void run() {
							location += 1;
							HashMap<String,String> map = AppCache.keyListMap.get(AppCache.PRICE).get(location);
							int infoId = Integer.parseInt(map.get(PriceBuyActivity.URL_OR_INFOID));
							Info info = InfoActivity.getInfoWithContentById(infoId,charset);
							if(null == info) return;
							String content = "<center><h3>"+info.getTitle()+"</h3>作者:"+info.getAuthor() + "   来源:"+info.getSource()+"</center>";
							content += info.getContent();
							CommonUtil.loadData(BrowsePriceActivity.this, content, "text/html", charset,webView, true, true, false);
						};
					}.start();
				}
			}
		});
	}
}
